<template>
  <div>
    <h1>{{title}}</h1>
    <el-select v-model="value" placeholder="请选择">
      <el-option v-for="item in options" :key="item.id" :label="item.title" :value="item.id"></el-option>
    </el-select>
    <el-upload class="upload-demo" :action="changeurl()" :on-success="asuccess">
      <el-button size="small" type="primary">点击上传</el-button>
      <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
    </el-upload>
  </div>
</template>
<script>
export default {
  data() {
    return {
      fileList: [],
      options: [],
      value: "",
      myurl: ""
    };
  },
  mounted: function() {
    const routerParams = this.$route.query.id;
    if (routerParams == "in") this.title = "模拟汽车进入停车场";
    else this.title = "模拟汽车出停车场";
    this.$ajax({
      method: "get",
      url: "park/area"
    }).then(res => {
      this.options = res.data.data;
    });
  },
  methods: {
    changeurl() {
      const routerParams = this.$route.query.id;
      if (routerParams == "in")
        return "http://db.hocent.cn:8080/api/park/in?area_id=" + this.value;
      else return "http://db.hocent.cn:8080/api/park/out?area_id=" + this.value;
    },
    asuccess(response, file, fileList) {
     // alert("success!");
      console.log(response)
       const routerParams = this.$route.query.id;
      if(response.error_msg!=null&&response.error_msg.indexOf("qrcodes") != -1)
      location.href = "http://139.155.39.205:8080/" + response.error_msg;
      else if(response.error_msg!=null){
        alert("您的车牌号为："+response.data);
      }
      alert(response.error_msg)
    
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);

      this.myurl = "park/in?area_id=" + this.value;
      this.$ajax.post(this.myurl, file);

      return false;
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${
          files.length
        } 个文件，共选择了 ${files.length + fileList.length} 个文件`
      );
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    }
  }
};
</script>